package com.itheima.web.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Course;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.CourseService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 该Servlet处理说有的CRUD操作
 * 分页查询: http://localhost:8080/heima_mm/store/course/findByPage?currentPage=1&pageSize=5;
 * 新增:http://localhost:8080/heima_mm/store/course/save
 * 新增:http://localhost:8080/heima_mm/store/course/edit
 * 新增:http://localhost:8080/heima_mm/store/course/delete
 *
 */
@WebServlet("/store/course/*")
public class CourseServlet extends BaseServlet {
       private CourseService courseService = BeanFactory.getBean("courseService", CourseService.class);

    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //调用service,根据id查询信息

            List<Course> list = courseService.findAll();
            //响应给过到客户端
            return new Result(true,MessageConstant.QUERY_COURSE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_COURSE_FAIL);
        }


    }
    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1. 获取请求参数并封装
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
            //2.调用service层方法,编辑数据
            courseService.delete(ids);
            //3.响应结果
            return new Result(true,MessageConstant.DELETE_COURSE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_COURSE_FAIL);
        }

    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            //1. 获取请求参数并封装
            Course course = BeanUtil.fillBeanFromJson(request, Course.class);
            //2.调用service层方法,编辑数据
            
            courseService.update(course);
            //3.响应结果
            return new Result(true,MessageConstant.EDIT_COURSE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_COURSE_FAIL);
        }


    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
            
        try {
            //获取请求参数 id
            String id = request.getParameter("id");
            //调用service,根据id查询信息

            Course course = courseService.findById(id);
            //响应给过到客户端
            return new Result(true,MessageConstant.QUERY_COURSE_SUCCESS,course);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_COURSE_FAIL);
        }


    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        try {
            //1. 获取请求参数并封装
            Course course = BeanUtil.fillBeanFromJson(request, Course.class);
            //2.调用service层方法,保存数据
            courseService.save(course);
            //3.响应结果
            return new Result(true,MessageConstant.SAVE_COURSE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_COURSE_FAIL);
        }

    }

    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //1.获取请求参数 currentPage和pageSize
            String currentPageSrt = request.getParameter("currentPage");
            String pageSizeSrt = request.getParameter("pageSize");
            //2.非空校验并赋默认值
            int currentPage=1,pageSize=5;
            if (!StringUtils.isBlank(currentPageSrt)){
                 currentPage = Integer.parseInt(currentPageSrt);
            }
            if (!StringUtils.isBlank(pageSizeSrt)){
                pageSize = Integer.parseInt(pageSizeSrt);
            }
            //3.调用service层方法,执行查询pageInfo
            PageInfo<Course> info = courseService.findByPage(currentPage, pageSize);
            //4.响应结果
            return new Result(true, MessageConstant.QUERY_COURSE_SUCCESS,info);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_COURSE_FAIL);
        }
    }
}
